package org.example.xsda.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import org.example.xsda.common.PageResult;
import org.example.xsda.common.Result;
import org.example.xsda.dto.ApprovalDTO;
import org.example.xsda.dto.ApprovalProcessDTO;
import org.example.xsda.dto.StudentApplyDTO;
import org.example.xsda.service.ApprovalService;
import org.example.xsda.vo.ApprovalDetailVO;
import org.example.xsda.vo.ApprovalVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/approval")
public class ApprovalController {
    
    @Resource
    private ApprovalService approvalService;
    
    @GetMapping("/page")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<PageResult<ApprovalVO>> getApprovalPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer applyType,
            @RequestParam(required = false) Integer status
    ) {
        PageResult<ApprovalVO> pageResult = approvalService.getApprovalPage(current, size, applyType, status);
        return Result.success(pageResult);
    }
    
    @PostMapping("/approve")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<Void> approve(@RequestBody ApprovalDTO dto) {
        approvalService.approve(dto);
        return Result.success();
    }
    
    @PostMapping("/reject")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<Void> reject(@RequestBody ApprovalDTO dto) {
        approvalService.reject(dto);
        return Result.success();
    }
    
    @GetMapping("/statistics")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<Map<String, Long>> getStatistics() {
        Map<String, Long> stats = approvalService.getStatistics();
        return Result.success(stats);
    }
    
    // ========== 学生异动申请相关接口 ==========
    
    /**
     * 学生提交异动申请
     */
    @PostMapping("/student/apply")
    public Result<Long> submitStudentApply(@RequestBody StudentApplyDTO dto) {
        Long approvalId = approvalService.submitStudentApply(dto);
        return Result.success(approvalId);
    }
    
    /**
     * 处理审批（初审/终审/转交）
     */
    @PostMapping("/process")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<Void> processApproval(@RequestBody ApprovalProcessDTO dto) {
        approvalService.processApproval(dto);
        return Result.success();
    }
    
    /**
     * 获取我的待审批列表
     */
    @GetMapping("/my-pending")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<PageResult<ApprovalVO>> getMyPendingApprovals(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size
    ) {
        PageResult<ApprovalVO> pageResult = approvalService.getMyPendingApprovals(current, size);
        return Result.success(pageResult);
    }
    
    /**
     * 获取审批详情（含学生档案快照、历史意见、附件列表）
     */
    @GetMapping("/detail/{id}")
    public Result<ApprovalDetailVO> getApprovalDetail(@PathVariable Long id) {
        ApprovalDetailVO detail = approvalService.getApprovalDetail(id);
        return Result.success(detail);
    }
    
    /**
     * 学生查询自己的申请记录
     */
    @GetMapping("/my-applies")
    @SaCheckRole("student")
    public Result<PageResult<ApprovalVO>> getMyApplies(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String subType,
            @RequestParam(required = false) Integer status
    ) {
        PageResult<ApprovalVO> pageResult = approvalService.getMyApplies(current, size, subType, status);
        return Result.success(pageResult);
    }
    
    /**
     * 撤回申请
     */
    @PostMapping("/cancel/{id}")
    @SaCheckRole("student")
    public Result<Void> cancelApproval(@PathVariable Long id) {
        approvalService.cancelApproval(id);
        return Result.success();
    }
}

